const path = require('path')
const __root__ = process.cwd()
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const WebpackAssetsManifest = require('webpack-assets-manifest');
const ManifestPlugin = require('webpack-manifest-plugin');
const {
	VueLoaderPlugin
} = require('vue-loader')
const wpModule = require('./config.module')

const {
	index,
	PATHS,
	template,
	publicPath,
	pathsToClean,
	cleanOptions,
	entry,
} = require('./utils')

const isProduction = process.env.NODE_ENV === 'production'

module.exports = {

	entry: entry(),

	output: {
		filename: PATHS.assets("js/[name].js"),
		chunkFilename: PATHS.assets('js/[name].[chunkhash].js'),
		path: PATHS.output,
		publicPath,
	},

	module: wpModule,

	resolve: {
		extensions: ['.js', '.json', 'jsm', '.css', '.less', '.scss', '.sass', '.jsx', '.vue'],
		alias: {
			'@': PATHS.src,
			'@ass': PATHS.resolveSrc('assets'),
			'@css': PATHS.resolveSrc('assets/css'),
			'@font': PATHS.resolveSrc('assets/font-awesome'),
			'@img': PATHS.resolveSrc('assets/images'),
			'@modules': PATHS.resolveSrc('modules'),
			'@utils': PATHS.resolveSrc('utils'),
			'@pl': PATHS.resolveSrc('vue-plugins'),
			'@coms': PATHS.resolveSrc('utils/commons'),
		},
	},

	plugins: [
		new CleanWebpackPlugin(pathsToClean, cleanOptions),

		// new VueLoaderPlugin(),

		new MiniCssExtractPlugin({
			// Options similar to the same options in webpackOptions.output
			// both options are optional
			// filename: PATHS.assets('css/[name].[hash].css'),
			filename: PATHS.assets('css/[name].css'),
			// chunkFilename: PATHS.assets('css/[name].[hash].css'),
		}),

		// new WebpackAssetsManifest({
		// 	publicPath: true,
		// 	// This enables us to include all relevant files for an entry
		// 	entrypoints: true,
		// 	// Also write to disk when using devServer
		// 	// instead of only keeping manifest.json in memory
		// 	// This is required to make devServer work with flask.
		// 	writeToDisk: true,
		// }),

		new ManifestPlugin({
			writeToFileEmit: true
		})

		// new HtmlWebpackPlugin({
		// 	filename: index,
		// 	// Required
		// 	inject: true,
		// 	// template: require('html-webpack-template'),
		// 	// template: 'index.html',
		// 	template,
		// 	// Optional
		// 	appMountId: 'app',
		// 	title: 'Dbmonitor Dashboard',
		// 	// favicon: path.join(PATHS.src, 'favicon.ico'),
		// 	meta: [{
		// 		name: 'description',
		// 		content: 'Bootstrap DB Monitor Dashboard'
		// 	}, {
		// 		name: 'keyword',
		// 		content: 'Dashboard, Bootstrap, Admin, Template, Theme, Responsive, Fluid, Retina'
		// 	}],
		// 	mobile: true,
		// 	lang: 'en-US',
		// 	inlineManifestWebpackName: 'webpackManifest',
		// 	minify: {
		// 		collapseWhitespace: true,
		// 		conservativeCollapse: true,
		// 		preserveLineBreaks: true,
		// 		useShortDoctype: true,
		// 		html5: true
		// 	}
		// }),
	],

}